import {login, logout, register} from '@/api/User'
import {getUserInfo} from "@/api/Wechat";

const user = {
    state: {
        // csrfToken: '',
        apps: [],
        // authorization: []
    },

    getters: {
        // csrfToken: state => state.csrfToken,
        apps: state => state.apps,
        // authorization: state => state.authorization
    },

    mutations: {
        // setCsrfToken(state, csrfToken){
        //     state.csrfToken = csrfToken
        // },

        // setAuthorization(state, authorization){
        //     state.authorization = authorization
        // },

        setApps(state, apps){
            state.apps = apps
        },
    },

    actions: {
        login({commit}, user){
            return new Promise((resolve, reject) => {
                login(user).then(res => {
                    var header = res.headers['access-token']
                    var user = res.data.data.user;
                    sessionStorage.setItem('jwt', header)
                    var s = JSON.stringify(user);
                    sessionStorage.setItem('userInfo', s)
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        register({commit}, userInfo){
            return new Promise((resolve, reject) => {
                register(userInfo).then(res => {
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        logout({commit}){
            return new Promise((resolve, reject) => {
                logout().then(res => {
                    sessionStorage.setItem('jwt', '')
                    sessionStorage.setItem('userInfo', {})
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        wechatLogin({commit}, unionId){
            return new Promise((resolve, reject) => {
                wechatLogin(token).then(res => {
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        }
    }
}

export default user